package com.zb.spring5.ioc.entity;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.GenericApplicationContext;

/**
 * 实现这个接口  可以拿到applicationContext
 * 这样可以自动注册关闭程序,从而调用destroy方法
 */
public class ShutDownHookBean implements ApplicationContextAware {

    private ApplicationContext ctx;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (applicationContext instanceof GenericApplicationContext){
            ((GenericApplicationContext) applicationContext).registerShutdownHook();
        }

    }
}
